package jdmx.framework.service;

import jdmx.framework.common.Envelope;
import jdmx.framework.common.interfaces.Facade;
import jdmx.library.helpers.TypeHelper;

public class ServiceFacade implements Facade {
	
	protected String getNamespace(String handlingCode) {
		return TypeHelper.getObjectPackageName(this);
	}
	
	protected Bo getBo(String handlingCode) {
		String packageName = getNamespace(handlingCode);
		return (Bo) ServiceContext.boFactory.createInstance(handlingCode, packageName);
	}

	protected Bt getBt(String handlingCode) {
		String packageName = getNamespace(handlingCode);
		return (Bt) ServiceContext.btFactory.createInstance(handlingCode, packageName);
	}
	
	public void init() {
		ServiceContext.init();
	}

	public Envelope execute(Envelope envelope) {
		return getBo(envelope.handlingCode).execute(envelope);
	}

	public Envelope run(Envelope envelope) {
		return getBt(envelope.handlingCode).run(envelope);
	}
}
